package org.javaboy.vhr.service;

import org.javaboy.vhr.mapper.MenuMapper;
import org.javaboy.vhr.mapper.MenuRoleMapper;
import org.javaboy.vhr.model.Hr;
import org.javaboy.vhr.model.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author Willie Chen
 * @Description
 * @Date 2019/10/13
 * @Param $param
 **/
@Service
public class MenuService {

    @Autowired
    MenuMapper menuMapper;

    @Autowired
    MenuRoleMapper menuRoleMapper;

    public List<Menu> getMenusByHrId(){

     return  menuMapper.getMenusByHrId(((Hr)
             SecurityContextHolder.getContext().getAuthentication()
             .getPrincipal()).getId());
    }
   // @Cacheable
    public  List<Menu> getAllMenusWithRole(){

        return  menuMapper.getAllMenusWithRole();
    }

    public List<Menu> getAllMenus() {

        return  menuMapper.getAllMenus();

    }

    public List<Integer> getMidsByRid(Integer rid) {


        return  menuMapper.getMidsByRid(rid);
    }

    @Transactional
    public boolean  updateMenuRole(Integer rid, Integer[] mids) {
//        if (mids == null) {
//
//         //   mids = new Integer[]{0, 1, 2};
//
//        }
          menuRoleMapper.deleteByRid(rid);
        Integer result = 0;
        if (mids != null) {
             result=menuRoleMapper.insertRecord(rid,mids);
            return  result==mids.length;
        }
        return true;
    }



}
